//
//  WSHeaderActView.m
//  LANGirl
//
//  Created by 杨泓晔 on 16/3/15.
//  Copyright © 2016年 杨泓晔. All rights reserved.
//

#import "WSHeaderActView.h"
#import "WSHeadActViewCell.h"
#import "AppDelegate.h"
@interface WSHeaderActView()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UIView *lineView;
@property (nonatomic, strong) UIButton *cancel;
@property (nonatomic, strong) NSArray *nameAry;
@property (nonatomic, strong) UIView *shareBackGroundView;
@property (nonatomic, strong) UIView *backgView;

@end

@implementation WSHeaderActView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.nameAry = [NSArray arrayWithObjects:@"拍摄照片", @"相册照片", @"默认照片", nil];
        self.backgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 160 * AUTO_WIDTH)];
        self.backgView.backgroundColor = [UIColor whiteColor];
        self.hidden = YES;
        
        [self createBackground];
        [self addSubview:_backgView];
        [self createSubView];
    }
    return self;
}

- (void)createSubView {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 3, self.backgView.frame.size.height - (50 * AUTO_WIDTH));
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.backgView.frame.size.height - (50 * AUTO_WIDTH)) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = WHITE;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.backgView addSubview:_collectionView];
    [self.collectionView registerClass:[WSHeadActViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self createLineView];
    [self createCancel];
}

- (void)createLineView {

    _lineView = [[UIView alloc] init];
    [self.backgView addSubview:_lineView];
    _lineView.backgroundColor = LINECOlOR;
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.collectionView.mas_bottom);
        make.left.right.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
    }];
}


- (void)createCancel {

    self.cancel = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.backgView addSubview:_cancel];
    [self.cancel setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancel setTitleColor:TextGrayColor forState:UIControlStateNormal];
    [self.cancel addTarget:self action:@selector(cancalAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.cancel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50 *AUTO_WIDTH));
        make.left.bottom.mas_equalTo(@0);
    }];
}

- (void)createBackground {
    
    UIView *background = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    background.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    background.opaque = NO;
    UIGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backgroundTapped:)];
    [background addGestureRecognizer:gesture];
    self.shareBackGroundView = background;
    
    [self addSubview:self.shareBackGroundView];
}

- (void)cancalAction:(UIButton *)btn {

    [self hiden];
}

#pragma mark - collection delegate && dataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    WSHeadActViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    if (indexPath.row == 0) {
        cell.image = [UIImage imageNamed:@"paizhao"];
    }
    if (indexPath.row == 1) {
        cell.image = [UIImage imageNamed:@"zhaopian"];
    }
    if (indexPath.row == 2) {
        cell.image = [UIImage imageNamed:@"moren"];
    }
    
    cell.nameStr = _nameAry[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        
        [self.delegate presentImagePicker:OPENCAMERA];
    } else if (indexPath.row == 1) {
        
        [self.delegate presentImagePicker:OPENPICS];
    } else {
        
        [self.delegate presentImagePicker:OPENDEFAULT];
    }
    [self hiden];
}

#pragma mark - 显示动画
- (void)show {

    self.shareBackGroundView.hidden = NO;
    self.hidden = NO;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.shareBackGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
        self.backgView.frame = CGRectMake(0, SCREEN_HEIGHT - (160 * AUTO_WIDTH) - 64, SCREEN_WIDTH, 160 * AUTO_WIDTH);
    }];
}

- (void)backgroundTapped:(UIGestureRecognizer *)tap {
    
    [self hiden];
}

- (void)hiden {

    self.hidden = YES;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.backgView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 160 * AUTO_WIDTH);
        self.shareBackGroundView.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
    } completion:^(BOOL finished) {
        
        self.shareBackGroundView.hidden = YES;
    }];
}

@end
